﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ElfTyping.States.GameState.Effects;

namespace ElfTyping.States.GameState.Entities
{
    public class SummonerEnemy : Enemy
    {
        const int CastingPositionX = 100;
        const int CastingDistance = 60;

        EnemyDifficulty Difficulty { get; set; }
        float TotalSecondsBetweenCasts { get; set; }
        float secondsUntilCast = 0;

        public Vector2 CastingCenterPosition
        {
            get { return new Vector2(CenterPosition.X + CastingDistance, CenterPosition.Y); }
        }

        public SummonerEnemy(GameScript script, Vector2 centerPosition, EnemyDifficulty difficulty)
            : base(script, centerPosition, difficulty)
        {
            switch (difficulty)
            {
                case EnemyDifficulty.Easy:
                    LoadAnimations(@"Textures\GameState\Summoner_Green");
                    MaximumHealth = 7;
                    TotalSecondsBetweenCasts = 6;
                    Damage = 0;
                    Experience = 3;
                    BaseSpeed = random.Next(10,20);
                    Word = GetNewWord(4);
                    break;

                case EnemyDifficulty.Medium:
                    LoadAnimations(@"Textures\GameState\Summoner_Purple");
                    MaximumHealth = 12;
                    TotalSecondsBetweenCasts = 7;
                    Damage = 0;
                    Experience = 6;
                    BaseSpeed = random.Next(10, 20);
                    Word = GetNewWord(5);
                    break;

                case EnemyDifficulty.Hard:
                    LoadAnimations(@"Textures\GameState\Summoner_Red");
                    MaximumHealth = 15;
                    TotalSecondsBetweenCasts = 8;
                    Damage = 0;
                    Experience = 10;
                    BaseSpeed = random.Next(10, 20);
                    Word = GetNewWord(6);
                    break;

            }

            Difficulty = difficulty;
            secondsUntilCast = TotalSecondsBetweenCasts / 2f; // first cast is twice as fast
        }

        public override void Tick(float dt)
        {
            if (CenterPosition.X < CastingPositionX) // moving until stop
            {
                MoveCenter(CenterPosition.X + MoveSpeed * dt, CenterPosition.Y);
                CurrentAnimation = MovingAnimation;
            }
            else if (secondsUntilCast > 0) // waiting to summon
            {
                BaseSpeed = 0;
                secondsUntilCast -= dt;
                CurrentAnimation = AttackingAnimation;
            }
            else // summoning
            {
                Script.SpawnEffect(new SummonSpellEffect(this));
                secondsUntilCast = TotalSecondsBetweenCasts;
            }

            base.Tick(dt);
        }

        public void SpawnSummon()
        {
            Script.SpawnEnemy(new BasicEnemy(Script.NewScript(), CastingCenterPosition, Difficulty));
            //Script.SpawnEffect(new SummonSpellEffect(this));
        }
    }
}
